package com.umakr.ax.opt.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.model.RadioStation;
import com.umakr.ax.common.model.WechatAccount;
import com.umakr.ax.common.service.AreaRsService;
import com.umakr.ax.common.service.AreaService;
import com.umakr.ax.common.service.RadioStationService;
import com.umakr.ax.common.service.WechatAccountService;
import com.umakr.ax.common.vo.RadioStationVO;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author umakr
 */
@Service
public class RadioStationOptService {
    @Autowired
    RadioStationService radioStationService;
    @Autowired
    AreaRsService areaRsService;
    @Autowired
    AreaService areaService;
    @Autowired
    UserOptService userOptService;
    @Autowired
    WechatAccountService wechatAccountService;

    @Value("${oss.img.path}")
    public String aliUrl;
    @Value("${native.resource.path}")
    public String localUrl;
    @Value("${user.default.avatar}")
    public String defaultAvatar;

    public Object listRadioStation(String rsName) {
        Page<RadioStationVO> page = radioStationService.listRadioStation(rsName);
        for (RadioStationVO item : page.getRecords()){
            item.setRsLogo(FormatPhotoUrlUtil.formatString(aliUrl,item.getRsLogo()));
        }
        return page;
    }

    public Object updateRadioStationStatus(Long id,Integer status) {
        int errorCode = 0 ;
        String msg ;
        Map<String,Object> data = Maps.newLinkedHashMap();
        radioStationService.updateStatusById(id,status);
        errorCode = 1 ;
        msg = "删除成功";
        data.put("result",1);
        return radioStationService.resultMap(data,errorCode,msg);
    }

    public Object saveOrUpdateRadioStation(RadioStation radioStation,String username,String password) {
        int errorCode = 0 ;
        String msg ;
        Map<String,Object> data = Maps.newLinkedHashMap();

        if(radioStation.getId() != null){
            radioStation.setUpdateTime(new Date());
            radioStationService.updateRadioStation(radioStation);
        }else {
            radioStation.setCreateTime(new Date());
            radioStationService.saveRadioStation(radioStation);
        }
        //处理管理员账号,默认管理员账号名字是：电台名+管理员
        String nickName = radioStation.getRsName()+"管理员";
        userOptService.addRsAdmin(username,password,nickName,radioStation.getId());

        data.put("id", radioStation.getId());
        data.put("rsName", radioStation.getRsName());
        data.put("rsDiscription", radioStation.getRsDiscription());
        data.put("rsLogo", radioStation.getRsLogo());
        data.put("rsSubordinateUnits", radioStation.getRsSubordinateUnits());
        data.put("rsStatus", radioStation.getRsStatus());
        data.put("rsLiveListenUrl", radioStation.getRsLiveListenUrl());
        data.put("rsLiveTelecastUrl", radioStation.getRsLiveTelecastUrl());
        data.put("rsHotlineTel", radioStation.getRsHotlineTel());
        data.put("createTime", radioStation.getCreateTime());
        data.put("rsCode", radioStation.getRsCode());
        data.put("contacts", radioStation.getContacts());
        data.put("contactsTell", radioStation.getContactsTell());
        data.put("cardInfo", radioStation.getCardInfo());
        data.put("businessLicenceInfo", radioStation.getBusinessLicenceInfo());
        data.put("accountNumber", radioStation.getAccountNumber());
        data.put("accountQrcodeUrl", radioStation.getAccountQrcodeUrl());
        data.put("rsAddress", radioStation.getRsAddress());

        errorCode = 1 ;
        msg = "操作成功";
        return radioStationService.resultMap(data,errorCode,msg);
    }

    public Object getRadioStationByRsId(Long rsId) {
        int errorCode = 0 ;
        String msg = "操作失败";
        Map<String,Object> data = Maps.newLinkedHashMap();

        RadioStation radioStation = radioStationService.queryById(rsId);

        if(radioStation != null){
            //查找微信公众账号是否已经审核通过
            EntityWrapper<WechatAccount> wechatAccountWrapper = new EntityWrapper<>();
            wechatAccountWrapper.eq("rs_id",radioStation.getId());
            Integer wechatAccountCount = wechatAccountService.selectCount(wechatAccountWrapper);

            data.put("wechatAccountBind", wechatAccountCount > 0 ? "1":"0");

            Map<String,Object> rsAdmin = radioStationService.getRsAdminByRsId(radioStation.getId());
            if(rsAdmin != null){
                data.put("account",rsAdmin.get("account"));
                data.put("password",rsAdmin.get("password"));
            }
            data.put("id", radioStation.getId());
            data.put("rsName", radioStation.getRsName());
            data.put("rsDiscription", radioStation.getRsDiscription());
            data.put("rsLogo", FormatPhotoUrlUtil.formatString(aliUrl,radioStation.getRsLogo()));
            data.put("rsLogoHad", radioStation.getRsLogo());
            data.put("rsSubordinateUnits", radioStation.getRsSubordinateUnits());
            data.put("rsStatus", radioStation.getRsStatus());
            data.put("rsLiveListenUrl", radioStation.getRsLiveListenUrl());
            data.put("rsLiveTelecastUrl", radioStation.getRsLiveTelecastUrl());
            data.put("rsHotlineTel", radioStation.getRsHotlineTel());
            data.put("createTime", radioStation.getCreateTime());
            data.put("rsCode", radioStation.getRsCode());
            data.put("contacts", radioStation.getContacts());
            data.put("contactsTell", radioStation.getContactsTell());
            data.put("cardInfo", FormatPhotoUrlUtil.formatJson(aliUrl,radioStation.getCardInfo()));
            data.put("cardInfoHad", radioStation.getCardInfo());
            data.put("businessLicenceInfo",FormatPhotoUrlUtil.formatJson(aliUrl, radioStation.getBusinessLicenceInfo()));
            data.put("businessLicenceInfoHad",radioStation.getBusinessLicenceInfo());
            data.put("accountNumber", radioStation.getAccountNumber());
            data.put("accountQrcodeUrl",FormatPhotoUrlUtil.formatString(aliUrl, radioStation.getAccountQrcodeUrl()));
            data.put("accountQrcodeUrlHad",radioStation.getAccountQrcodeUrl());
            data.put("rsAddress", radioStation.getRsAddress());
            errorCode = 1 ;
            msg = "操作成功";
        }
        return radioStationService.resultMap(data,errorCode,msg);
    }
}
